package cn.iocoder.yudao.module.resource.service.download;

import cn.iocoder.yudao.module.resource.dal.dataobject.download.ResourceDownloadDO;
import cn.iocoder.yudao.module.resource.dal.mysql.download.ResourceDownloadMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 下载记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class ResourceDownloadServiceImpl implements ResourceDownloadService {

    @Resource
    private ResourceDownloadMapper downloadMapper;

    @Override
    public Long createDownload(ResourceDownloadDO downloadDO) {
        downloadMapper.insert(downloadDO);
        return downloadDO.getId();
    }

    @Override
    public ResourceDownloadDO getDownload(Long id) {
        return downloadMapper.selectById(id);
    }

    @Override
    public ResourceDownloadDO getDownloadByUserIdAndMaterialId(Long userId, Long materialId) {
        return downloadMapper.selectByUserIdAndMaterialId(userId, materialId);
    }

    @Override
    public List<ResourceDownloadDO> getDownloadListByUserId(Long userId) {
        return downloadMapper.selectListByUserId(userId);
    }

    @Override
    public boolean hasDownloaded(Long userId, Long materialId) {
        return getDownloadByUserIdAndMaterialId(userId, materialId) != null;
    }

}
